package com.home.controller;

import com.home.config.minioConfig.MinioConfig;
import com.home.result.Result;
import com.home.utils.MinioUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;

/**
 * @author 小飞
 */
@RestController
@RequestMapping("/home/api")
@Slf4j
@Api(tags = "文件管理模块")
public class UploadController {

    @Autowired
    private MinioUtil minioUtil;

    @Autowired
    private MinioConfig minioConfig;

    /**
     * 文件上传
     *
     * @param file
     */
    @PostMapping("/upload")
    @ApiOperation("文件上传")
    public Result<?> upload(@RequestParam("file") MultipartFile file) {
        try {
            //文件名
            String fileName = file.getOriginalFilename();
            String newFileName = System.currentTimeMillis() + "." + StringUtils.substringAfterLast(fileName, ".");
            //类型
            String contentType = file.getContentType();
            minioUtil.uploadFile(minioConfig.getBucketName(), file, newFileName, contentType);
            return Result.success(null,newFileName);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed("上传失败");
        }
    }

    /**
     * 删除
     *
     * @param fileName
     */
    @ApiOperation("删除文件")
    @DeleteMapping("/")
    public Result<?> delete(@RequestParam("fileName") String fileName) {
        minioUtil.removeFile(minioConfig.getBucketName(), fileName);
        return Result.success("删除成功");
    }

    /**
     * 获取文件信息
     *
     * @param fileName
     * @return
     */
    @ApiOperation("获取文件信息")
    @GetMapping("/info")
    public Result<?> getFileStatusInfo(@RequestParam("fileName") String fileName) {
        String fileStatusInfo = minioUtil.getFileStatusInfo(minioConfig.getBucketName(), fileName);
        return Result.success(null,fileStatusInfo);

    }

    /**
     * 获取文件外链
     *
     * @param fileName
     * @return
     */
    @ApiOperation("浏览文件")
    @GetMapping("/url")
    public Result<?> getPresignedObjectUrl(@RequestParam("fileName") String fileName) {
        String presignedObjectUrl = minioUtil.getPresignedObjectUrl(minioConfig.getBucketName(), fileName);
        return Result.success(null,presignedObjectUrl);
    }

    /**
     * 文件下载
     *
     * @param fileName
     * @param response
     */
    @ApiOperation("文件下载")
    @GetMapping("/download")
    public Result<?> download(@RequestParam("fileName") String fileName, HttpServletResponse response) {
        try {
            InputStream fileInputStream = minioUtil.getObject(minioConfig.getBucketName(), fileName);
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.setContentType("application/force-download");
            response.setCharacterEncoding("UTF-8");
            IOUtils.copy(fileInputStream, response.getOutputStream());
            return Result.success();
        } catch (Exception e) {
            log.error("下载失败");
            return Result.failed("下载失败");
        }
    }
}